<?php

class advice extends CI_Controller {
	
	function __construct ()
	{
		parent::__construct();	
		$this->pre_message = "";
        $this->load->model('advice_model','advice');
	}
	
	function index()
	{
		$this->listadvice();
	}

	function listadvice()
	{
		$data['title'] = 'Danh sách';
		$data['add'] = 'advice/addnew';
		$config['base_url'] = base_url().'advice/listadvice/'.(int)$this->uri->segment(3); 
		$data['num'] = $this->advice->count_advice_list();
		$config['total_rows']   =  $data['num'];
		$config['per_page']  =   '20' ;
		$config['uri_segment'] = 4 ;   
		$this->pagination->initialize($config);   
		$data['list'] =   $this->advice->get_advice_list($config['per_page'],(int)$this->uri->segment('4'));
		$data['pagination']    = $this->pagination->create_links();            
		$this->_templates['page'] = 'advice/index';
		$this->site->load($this->_templates['page'],$data);
		
	}
		
	function addnew()
	{
		$data['title'] = 'Thêm about mới';
		$this->form_validation->set_rules('subject_vn','Tiêu đề','trim|required');
		$this->form_validation->set_rules('ks_image','','');
		$this->form_validation->set_rules('subcontent_vn','','');
		$this->form_validation->set_rules('content_vn','','');
		
		if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
		}else
		{
		  if($this->advice->save())
		{
			  $this->session->set_flashdata('message','Lưu thành công');
			  redirect('advice/listadvice');
		  }
		}
		
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'advice/add';
		$this->site->load($this->_templates['page'],$data);
	}
	function update()
	{
		$id = (int)$this->uri->segment(3);                              
		$data['rs'] = $this->advice->get_advice($id);
		$data['title'] = 'Cập nhật: '.$data['rs']->subject_vn;
		if(!empty($_POST['bt_submit']))
		{
		$this->form_validation->set_rules('subject_vn','Tiêu đề','trim|required');
		$this->form_validation->set_rules('subcontent_vn','','');
		$this->form_validation->set_rules('content_vn','','');
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				if($this->advice->save())
				{
					$this->session->set_flashdata('message','Cập nhật chuyên mục thành công');
					redirect('advice');
				}
		 	}
		}  
		$this->_templates['page'] = 'advice/update';
		$this->site->load($this->_templates['page'],$data);
	}
			
	function del()//hien thi thong bao va update lai
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->advice->delete($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		
		redirect('advice/listadvice/'.$page);
	}

	function delsadvice()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->advice->delsabout($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('advice/listadvice/'.$page);
	}

}
?>